package com.dream.controller;

import com.dream.assisitant.Assistant;
import com.dream.assisitant.IsolationChatAssistant;
import com.dream.assisitant.MemoryChatAssistant;
import com.dream.assisitant.StreamAssistant;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.service.AiServices;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @description: 聊天Controller
 * @author: LiMG
 * @create: 2025-07-02 17:06:42
 **/
@Tag(name = "Lesson002接口", description = "Lesson002接口")
@RestController
@RequestMapping("/lesson002")
public class ChatController {

    @Autowired
    private QwenChatModel qwenChatModel;

    @Operation(summary = "AiServices创建聊天")
    @GetMapping("/chat")
    public String chat(@RequestParam("prompt") String prompt) {
        QwenChatModel model = QwenChatModel.builder()
                .apiKey(System.getenv("BAI_LIAN_KEY"))
                .modelName("qwen-max")
                .build();
        Assistant assistant = AiServices.create(Assistant.class, model);
        return assistant.chat(prompt);
    }

    @Autowired
    private Assistant assistant;

    @Operation(summary = "注解创建聊天")
    @GetMapping("/chat2")
    public String chat2(@RequestParam("prompt") String prompt) {
        return assistant.chat(prompt);
    }

    private Assistant aiAssistant;
    @Operation(summary = "聊天记忆")
    @GetMapping("/chat3")
    public String chat3(@RequestParam("prompt") String prompt) {
        if (null == aiAssistant) {
            MessageWindowChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
            aiAssistant = AiServices.builder(Assistant.class)
                    .chatModel(qwenChatModel)
                    .chatMemory(chatMemory)
                    .build();
        }
        return aiAssistant.chat(prompt);
    }


    @Autowired
    private MemoryChatAssistant memoryChatAssistant;
    @Operation(summary = "AiService实现聊天记忆")
    @GetMapping("/chat4")
    public String chat4(@RequestParam("prompt") String prompt) {
        return memoryChatAssistant.chat(prompt);
    }

    @Autowired
    private IsolationChatAssistant isolationChatAssistant;

    @Operation(summary = "聊天记忆隔离")
    @GetMapping("/chat5")
    public String chat5(@RequestParam("memoryId") String memoryId,
                        @RequestParam("prompt") String prompt) {
        return isolationChatAssistant.chat(memoryId, prompt);
    }

    @Autowired
    private StreamAssistant streamAssistant;
    @Operation(summary = "Flux流式聊天")
    @GetMapping(value = "/chat6", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> chat6(@RequestParam("prompt") String prompt) {
        return streamAssistant.chat(prompt);
    }

}
